﻿// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.TextFormatting;

using ICSharpCode.AvalonEdit.Rendering;
using ICSharpCode.AvalonEdit.Utils;

namespace ICSharpCode.AvalonEdit.Folding
{
	/// <summary>
	/// A <see cref="VisualLineElementGenerator"/> that produces line elements for folded <see cref="FoldingSection"/>s.
	/// </summary>
	public sealed class FoldingElementGenerator : VisualLineElementGenerator, ITextViewConnect
	{
		readonly List<TextView> textViews = new List<TextView>();
		FoldingManager foldingManager;

		#region FoldingManager property / connecting with TextView
		/// <summary>
		/// Gets/Sets the folding manager from which the foldings should be shown.
		/// </summary>
		public FoldingManager FoldingManager {
			get {
				return foldingManager;
			}
			set {
				if (foldingManager != value) {
					if (foldingManager != null) {
						foreach (TextView v in textViews)
							foldingManager.RemoveFromTextView(v);
					}
					foldingManager = value;
					if (foldingManager != null) {
						foreach (TextView v in textViews)
							foldingManager.AddToTextView(v);
					}
				}
			}
		}

		void ITextViewConnect.AddToTextView(TextView textView)
		{
			textViews.Add(textView);
			if (foldingManager != null)
				foldingManager.AddToTextView(textView);
		}

		void ITextViewConnect.RemoveFromTextView(TextView textView)
		{
			textViews.Remove(textView);
			if (foldingManager != null)
				foldingManager.RemoveFromTextView(textView);
		}
		#endregion

		/// <inheritdoc/>
		public override void StartGeneration(ITextRunConstructionContext context)
		{
			base.StartGeneration(context);
			if (foldingManager != null) {
				if (!foldingManager.textViews.Contains(context.TextView))
					throw new ArgumentException("Invalid TextView");
				if (context.Document != foldingManager.document)
					throw new ArgumentException("Invalid document");
			}
		}

		/// <inheritdoc/>
		public override int GetFirstInterestedOffset(int startOffset)
		{
			if (foldingManager != null) {
				foreach (FoldingSection fs in foldingManager.GetFoldingsContaining(startOffset)) {
					// Test whether we're currently within a folded folding (that didn't just end).
					// If so, create the fold marker immediately.
					// This is necessary if the actual beginning of the fold marker got skipped due to another VisualElementGenerator.
					if (fs.IsFolded && fs.EndOffset > startOffset) {
						//return startOffset;
					}
				}
				return foldingManager.GetNextFoldedFoldingStart(startOffset);
			} else {
				return -1;
			}
		}

		/// <inheritdoc/>
		public override VisualLineElement ConstructElement(int offset)
		{
			if (foldingManager == null)
				return null;
			int foldedUntil = -1;
			FoldingSection foldingSection = null;
			foreach (FoldingSection fs in foldingManager.GetFoldingsContaining(offset)) {
				if (fs.IsFolded) {
					if (fs.EndOffset > foldedUntil) {
						foldedUntil = fs.EndOffset;
						foldingSection = fs;
					}
				}
			}
			if (foldedUntil > offset && foldingSection != null) {
				// Handle overlapping foldings: if there's another folded folding
				// (starting within the foldingSection) that continues after the end of the folded section,
				// then we'll extend our fold element to cover that overlapping folding.
				bool foundOverlappingFolding;
				do {
					foundOverlappingFolding = false;
					foreach (FoldingSection fs in FoldingManager.GetFoldingsContaining(foldedUntil)) {
						if (fs.IsFolded && fs.EndOffset > foldedUntil) {
							foldedUntil = fs.EndOffset;
							foundOverlappingFolding = true;
						}
					}
				} while (foundOverlappingFolding);

				string title = foldingSection.Title;
				if (string.IsNullOrEmpty(title))
					title = "...";
				var p = new VisualLineElementTextRunProperties(CurrentContext.GlobalTextRunProperties);
				p.SetForegroundBrush(textBrush);
				var textFormatter = TextFormatterFactory.Create(CurrentContext.TextView);
				var text = FormattedTextElement.PrepareText(textFormatter, title, p);

				#region lme
				Brush foldingBackground = Brushes.DarkCyan;

				//var regForImageLinkLineText = new Regex(@"^[ ]{0,3}[!！]\[.*$");
				//var matchForImageLinkLineText = regForImageLinkLineText.Match(title);
				//if (matchForImageLinkLineText.Success) foldingBackground = Brushes.Crimson;

				//var regForFileLinkLineText = new Regex(@"^[ ]{0,3}\[.*$");
				//var matchForFileLinkLineText = regForFileLinkLineText.Match(title);
				//if (matchForFileLinkLineText.Success) foldingBackground = Brushes.SaddleBrown;

				if (FoldingColorRules.Count > 0) {
					foreach (var rule in FoldingColorRules) {
						if (string.IsNullOrEmpty(rule.RegexText)) continue;

						Regex regex = new Regex(rule.RegexText);
						var match = regex.Match(title);
						if (match.Success) {
							textBrush = rule.Foreground;
							foldingBackground = rule.Background;
							break;
						}
					}
				}
				//自定义折叠区背景色（折叠区底色）。

				return new FoldingLineElement(foldingSection, text, foldedUntil - offset) {
					textBrush = textBrush,
					backgroundBrush = foldingBackground,
					//如果是图像链接文本被折叠，使用DarkCyan为背景；
					//如果是其它折叠块，使用猩红色为背景。
				};
				//return new FoldingLineElement(foldingSection, text, foldedUntil - offset) { textBrush = textBrush };
				#endregion
			} else {
				return null;
			}
		}

		#region lme
		/// <summary>
		/// 用以决定折叠时色彩的规则列表。
		/// </summary>
		public static List<FoldingColorRule> FoldingColorRules = new List<FoldingColorRule>();
		#endregion

		sealed class FoldingLineElement : FormattedTextElement
		{
			readonly FoldingSection fs;

			internal Brush textBrush;

			#region lme
			internal Brush backgroundBrush;
			#endregion

			public FoldingLineElement(FoldingSection fs, TextLine text, int documentLength) : base(text, documentLength)
			{
				this.fs = fs;
			}

			public override TextRun CreateTextRun(int startVisualColumn, ITextRunConstructionContext context)
			{
				return new FoldingLineTextRun(this, this.TextRunProperties) { textBrush = textBrush };
			}

			protected internal override void OnMouseDown(MouseButtonEventArgs e)
			{
				if (e.ClickCount == 2 && e.ChangedButton == MouseButton.Left) {
					fs.IsFolded = false;
					e.Handled = true;
				} else {
					base.OnMouseDown(e);
				}
			}
		}

		sealed class FoldingLineTextRun : FormattedTextRun
		{
			internal Brush textBrush;

			public FoldingLineTextRun(FormattedTextElement element, TextRunProperties properties)
				: base(element, properties)
			{
			}

			public override void Draw(DrawingContext drawingContext, Point origin, bool rightToLeft, bool sideways)
			{
				var metrics = Format(double.PositiveInfinity);
				Rect r = new Rect(origin.X, origin.Y - metrics.Baseline, metrics.Width, metrics.Height);
				//drawingContext.DrawRectangle(null, new Pen(textBrush, 1), r);
				drawingContext.DrawRoundedRectangle(backgroundBrush, new Pen(textBrush, 0), r, 4, 4);
				base.Draw(drawingContext, origin, rightToLeft, sideways);
			}
		}

		#region lme 更改折叠标头默认前景色
		/// <summary>
		/// Default brush for folding element text. Value: Brushes.White
		/// </summary>
		public static readonly Brush DefaultTextBrush = Brushes.White;  // Brushes.Gray.
		#endregion

		static Brush textBrush = DefaultTextBrush;

		/// <summary>
		/// Gets/sets the brush used for folding element text.
		/// </summary>
		public static Brush TextBrush {
			get { return textBrush; }
			set { textBrush = value; }
		}

		#region lme
		/// <summary>
		/// Default background brush for folding element text. Value: Brushes.Crimson
		/// </summary>
		public static readonly Brush DefaultBackgroundBrush = Brushes.Crimson;

		static Brush backgroundBrush = DefaultBackgroundBrush;
		#endregion
	}

	#region lme
	/// <summary>
	/// 此类用以提供自定义的正则表达式和对应的前景、背景色来对折叠区在折叠状态下的色彩进行定义。
	/// </summary>
	public class FoldingColorRule
	{
		/// <summary>
		/// 正则表达式文本。
		/// </summary>
		public string RegexText { get; set; } = "";
		/// <summary>
		/// 折叠块前景色。默认为 White。
		/// </summary>
		public Brush Foreground { get; set; } = Brushes.White;
		/// <summary>
		/// 折叠块背景色。默认为 SaddleBrown。
		/// </summary>
		public Brush Background { get; set; } = Brushes.SaddleBrown;
	}
	#endregion
}
